define RUN_CMD
	@printf "\033[;31m"; printf $1; printf "\033[0m\n"
	@sh -c $1
endef

PROJ_NAME=silentsea
TARGET_PLATFORM=android-10
NDK_MODULE_PATH = $(CURDIR)/cocos2d:$(CURDIR)/cocos2d/cocos:$(CURDIR)/cocos2d/external

debug:
	$(call RUN_CMD, "ndk-build -j2 -C $(CURDIR)/proj.android NDK_MODULE_PATH=$(NDK_MODULE_PATH) NDK_DEBUG=1")
	$(call RUN_CMD, "android update lib-project -t $(TARGET_PLATFORM) -p $(CURDIR)/cocos2d/cocos/2d/platform/android/java")
	$(call RUN_CMD, "android update project -t $(TARGET_PLATFORM) -p $(CURDIR)/proj.android -s")
	$(call RUN_CMD, "mkdir -p $(CURDIR)/proj.android/assets && rsync -aI --exclude='.*' $(CURDIR)/Resources/ $(CURDIR)/proj.android/assets")
	$(call RUN_CMD, "ant clean debug -f $(CURDIR)/proj.android/build.xml")

release:
	$(call RUN_CMD, "ndk-build -j2 -C $(CURDIR)/proj.android NDK_MODULE_PATH=$(NDK_MODULE_PATH)")
	$(call RUN_CMD, "android update lib-project -t $(TARGET_PLATFORM) -p $(CURDIR)/cocos2d/cocos/2d/platform/android/java")
	$(call RUN_CMD, "android update project -t $(TARGET_PLATFORM) -p $(CURDIR)/proj.android -s")
	$(call RUN_CMD, "mkdir -p $(CURDIR)/proj.android/assets && rsync -aI --exclude='.*' $(CURDIR)/Resources/ $(CURDIR)/proj.android/assets")
	$(call RUN_CMD, "ant clean release -f $(CURDIR)/proj.android/build.xml")

install:
	$(call RUN_CMD, "ant installd -f $(CURDIR)/proj.android/build.xml")

init:
	$(call RUN_CMD, "rm -rf $(CURDIR)/$(PROJ_NAME)")
	$(call RUN_CMD, "rm -rf $(CURDIR)/proj.android")
	$(call RUN_CMD, "rm -rf $(CURDIR)/cocos2d")
	$(call RUN_CMD, "cocos new $(PROJ_NAME) -p com.brickred.$(PROJ_NAME) -l cpp -d .")
	$(call RUN_CMD, "mv $(CURDIR)/$(PROJ_NAME)/cocos2d .")
	$(call RUN_CMD, "mv $(CURDIR)/$(PROJ_NAME)/proj.android .")
	$(call RUN_CMD, "rm -rf $(CURDIR)/$(PROJ_NAME)")
	$(call RUN_CMD, "cp $(CURDIR)/tools/Application.mk $(CURDIR)/proj.android/jni")
	$(call RUN_CMD, "cp $(CURDIR)/tools/Android.mk $(CURDIR)/proj.android/jni")
	$(call RUN_CMD, "sed -i -f $(CURDIR)/tools/AndroidManifest.xml.sed $(CURDIR)/proj.android/AndroidManifest.xml")

purge:
	$(call RUN_CMD, "rm -rf $(CURDIR)/$(PROJ_NAME)")
	$(call RUN_CMD, "rm -rf $(CURDIR)/proj.android")
	$(call RUN_CMD, "rm -rf $(CURDIR)/cocos2d")

clean:
	$(call RUN_CMD, "ndk-build -C $(CURDIR)/proj.android NDK_MODULE_PATH=$(NDK_MODULE_PATH) clean")
	$(call RUN_CMD, "ant clean -f $(CURDIR)/proj.android/build.xml")
	$(call RUN_CMD, "rm -rf $(CURDIR)/proj.android/obj/*")
	$(call RUN_CMD, "rm -rf $(CURDIR)/proj.android/libs/*")

gdb:
	cd proj.android && NDK_MODULE_PATH=$(NDK_MODULE_PATH) ndk-gdb --start

